python短代码示例

13个简单的python小代码

1. 华氏度/摄氏度 转换

1
2
3
4
5
6
7
8
9
10
# 温度换算
temp_str = input('请输入带有符号的温度值:')
if temp_str[-1] in ['F', 'f']:
c = (eval(temp_str[:-1]) - 32)/1.8
print('转换后的温度是{:.2f}C'.format(c))
elif temp_str[-1] in ['C', 'c']:
f = 1.8*eval(temp_str[:-1])+32
print('转换后的温度是{:.2f}f'.format(f))
else:
print('输入格式错误')

2. 日志文件分析

传感器日志文件,系统管理日志文件,用户行为日志文件,安全日志文件,网络流量日志文件,批量结果日志文件,数据库访问日志文件,访问控制日志文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#   日期Date | 时间Time | 温度Temperature | 湿度Humidity | 光照Light | 电压Voltage
# yyyy-mm-dd hh:mm:ss.xxx real real real real
# 日志文件中,每行是一条日志信息
# 每行日志包括4个传感器数据:温度、湿度、光照和电压

# 输入:日志文件 sensor-data.txt
# 输出:平均温度数值,保留小数点两位
try:
f = open('sensor-data.txt', 'r')
total , cnt =0,0
for line in f:
ls = line.split()
cnt += 1
total += eval(ls[2])

print('平均温度是:{:.2f}'.format(total/cnt))
f.close()
except:
print('文件打开错误')

3. 中文文本词频统计

1
2
3
4
5
6
7
8
9
10
11
import jieba
with open('file.txt' ,'r', encoding='utf-8') as f:
text = f.read()
ls = jieba.lcut(text)
d = {}
for word in ls:
if word not in ['\n', ',', '.']:
d[word] = d.get(word, 0) + 1

for k in d:
print('"{}"出现{}次'.format(k, d[k]))

4. 自然常数e的计算

$$
e = \lim_{x\rightarrow\infty}(1+\frac{1}{x})^x \approx 2.71828 18284 59045 23536
$$

常规方法

1
2
3
In [1]: x = 1024**4
In [2]: pow((1+1/x), x)
Out[2]: 2.718281828457809

蒙特卡洛方法:

已知
$$
\int_1^2\frac{1}{x}dx = ln2 = log_e2
$$

可以用随机法确定ln2的大小,然后反推e

$$
e = \lim_{x\rightarrow\infty}(1+\frac{1}{2})^x
$$

1
2
3
4
5
6
7
8
9
10
11
from random import uniform

DARTS = 1024**2
count = 0
for i in range(DARTS):
x = uniform(1, 2)
y = uniform(0, 1)
if x*y < 1:
count += 1
e = pow(2, DARTS/count)
print(e)

5. 文本清洗及统计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 在中国参与MOOC建设的大学有多少所
# 提取其中大学信息,输出空格分隔的大学名称
import requests
url = 'https://www.icourse163.org/university/view/all.html/'
html = requests.get(url).text
ls = []
for line in html:
if "alt" in line:
tokens = line.split('"')
uname = tokens[-2]
if "大学生" in uname:
continue
if "大学" in uname or "学院" in uname:
ls.append(uname)
print(" ".join(ls))
print(len(ls))
fi.close()

6. 蒙特卡洛猜测与计时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 随机产生字符串,匹配特定的正则表达式,统计匹配时间和匹配次数
# 模拟病毒引擎的扫描过程
import time, random, re
def genStr():
global sigma
s = ""
for i in range(32):
s += sigma[random.randint(0, 15)]
return s
sigma = "0123456789ABCDEF"
regex = re.compile(r'[1-2][^2-8][D-F]0+[A-F]')
count = 0
start = time.perf_counter()
match = regex.search(genStr())
while not match:
count += 1
match = regex.search(genStr())
print("程序匹配: 猜测{}次,{}->{}".format(count, match.string, match.group(0)))
end = time.perf_counter()
print("程序用时:{:.5f}秒".format(end-start))

7. 四大名著词云分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 输入:四大名著的txt文本文件
# 输出:词云效果及有形状的词云
import jieba
import wordcloud
names = {"红楼梦.txt", "三国演义.txt", "水浒传.txt", "西游记.txt"}
for name in names:
f = open(name, "r", encoding="utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud( font_path = "msyh.ttc",width = 1000, height = 700)
w.generate(txt)
w.to_file(name.split(".")[0] + ".png")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import jieba
import wordcloud
from scipy.misc import imread
mask = imread("sun.png")
names = {"红楼梦.txt", "三国演义.txt", "水浒传.txt", "西游记.txt"}
for name in names:
f = open(name, "r", encoding="utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud( font_path = "msyh.ttc",width = 1000, height = 700, mask = mask)
w.generate(txt)
w.to_file(name.split(".")[0] + "2.png")

8. 金庸武侠写作风格分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 统计金庸小说中的字频和词频
#飞雪连天射白鹿 笑书神侠倚碧鸳

fnames = {"飞狐外传", "雪山飞狐", "连城诀", "天龙八部", "射雕英雄传", "白马啸西风", "鹿鼎记", "笑傲江湖", "书剑恩仇录", "神雕侠侣", "侠客行", "倚天屠龙记", "碧血剑", "鸳鸯刀"}


def PrintJYChars( fname ):
txt = open( fname , "r").read()
d,cnt,rst = {},0,''
for w in txt:
cnt += 1
d[w] = d.get(w, 0) + 1
for w in ",。“”:?\n 「」∶":
try :
del d[w]
except :
pass
ls = list(d.items())
ls.sort(key= lambda x :x[1], reverse= True )
for i in range(20):
word, count = ls[i]
rst += word
print(rst)
return rst


txt = PrintJYChars("天龙八部" + ".txt")
A = set(txt.split("\n")[-1])
for fname in fnames:
txt = PrintJYChars(fname + ".txt")
A &= set(txt.split("\n")[-1])
print(A)


import jieba
def PrintJYWords( fname ):
txt = open( fname , "r").read()
d,cnt,rst = {},0,''
for w in jieba.lcut(txt):
cnt += 1
if len(w) == 1:
continue
d[w] = d.get(w, 0) + 1
ls = list(d.items())
ls.sort(key= lambda x :x[1], reverse= True )
for i in range(50):
word, count = ls[i]
rst += word + ","
return rst

txt = PrintJYWords("天龙八部" + ".txt")
A = set(txt.strip(",").split(","))
for fname in fnames:
txt = PrintJYWords(fname + ".txt")
A &= set(txt.split(","))
print(A)

9. 银行ATM等待时间分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'''
对于特定客户流,用户平均等待时间是多少
ATM机:由于不同业务,处理时间符合一定的随机分布
客户流:由于不同环境,客户到达符合一定的随机分布
平均等待时间:每个客户平均的等待时间
'''
import random


class ATM(object):
def __init__(self, maxtime=5):
self.t_max = maxtime

def get_serv_complete_time(self, start=0):
return start + random.randint(1, self.t_max)


class Customers(object):
def __init__(self, n):
self.count = n
self.left = n

def get_next_arrv_time(self, start=0, arrv_time=10):
if self.left != 0:
self.left -= 1
return start + random.randint(1, arrv_time)
else:
return 0

def is_over(self):
return True if self.left == 0 else False


c = Customers(100)
a = ATM()

wait_list = []
wait_time, cur_time = 0, 0

cur_time += c.get_next_arrv_time()
wait_list.append(cur_time)

while len(wait_list) != 0 or not c.is_over():
if wait_list[0] <= cur_time:
next_time = a.get_serv_complete_time(cur_time)
del wait_list[0]
else:
next_time = cur_time + 1

if not c.is_over() and len(wait_list) == 0:
next_arrv = c.get_next_arrv_time(cur_time)
wait_list.append(next_arrv)

if not c.is_over() and wait_list[-1] < next_time:
next_arrv = c.get_next_arrv_time(wait_list[-1])
wait_list.append(next_arrv)
while next_arrv < next_time and not c.is_over():
next_arrv = c.get_next_arrv_time(next_arrv)
wait_list.append(next_arrv)

for i in wait_list:
if i <= cur_time:
wait_time += next_time - cur_time
elif cur_time < i < next_time:
wait_time += next_time - i
else:
pass
cur_time = next_time
print(wait_time / c.count)

10. 图像的四则运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 加减法:两个图像相加减
# 乘除法:一个图像与一个数字之间的乘除法
import numpy as np
from PIL import Image


class ImageObject(object):
def __init__(self, path=''):
self.path = path
try:
self.data = np.array(Image.open(path))
except:
self.data = None

def __add__(self, other):
image = ImageObject()
try:
image.data = np.mod(self.data + other.data, 255)
except:
image.data = self.data
return image

def __sub__(self, other):
image = ImageObject()
try:
image.data = np.mod(self.data - other.data, 255)
except:
image.data = self.data
return image

def __mul__(self, other):
image = ImageObject()
try:
image.data = np.mod(self.data * other, 255)
except:
image.data = self.data
return image

def __truediv__(self, other):
image = ImageObject()
try:
image.data = np.mod(self.data // other, 255)
except:
image.data = self.data
return image

def saveImage(self, path):
try:
im = Image.fromarray(self.data)
im.save(path)
return True
except:
return False


a = ImageObject('a.jpg')
b = ImageObject('b.png')
(a + b).saveImage('add.png')
(a - b).saveImage('sub.png')
(a * 2).saveImage('mul.png')
(a / 2).saveImage('div.png')

11. 矩阵乘法模块的构建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 矩阵乘法
# A(nrow, nk) 乘 B(nk, ncol) 得 C(nrow, ncol)
# 矩阵乘法是人工智能算法的核心运算之一,是数据分析的数据分析表达及运算之一,是大规模科学计算的核心操作之一
# 输入:两个矩阵及行列值
def mxmul(mx1, mx2, nrow, nk, ncol):
rst = [[0 for i in range(ncol)] for j in range(nrow)]
for i in range(nrow):
for j in range(nrow):
for k in range(nk):
rst[i][j] += mx1[i][k] * mx2[k][j]
return rst


if __name__ == '__main__':
import time

nrow, nk, ncol = 500, 300, 500
mx1 = [[i for i in range(nk)] for j in range(nrow)]
mx2 = [[i for i in range(ncol)] for j in range(nk)]
start = time.perf_counter()
rst = mxmul(mx1, mx2, nrow, nk, ncol)
end = time.perf_counter()
print('运行时间:%.4f秒' % (end - start))

'''
运行时间:25.3109秒
'''

12. 矩阵乘法的C语言加速

1
2
3
4
5
6
7
8
9
10
11
12
13
/* 可编译为.dll的C语言代码  mxmul.h */
#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
#define DLLIMPORT __declspec(dllexport)
#else
#define DLLIMPORT __declspec(dllimport)
#endif

DLLIMPORT int *mxmul(int nrow, int nk, int ncol, int mx1[][nk], int mx2[][ncol]);

#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* 可编译为.dll的C语言代码  mxmul.c */
#include"mxmul.h"
#include<windows.h>
#include<stdlib.h>

/* Parameters: nrow, nk, ncol, mx1, mx2 */
DLLIMPORT int *mxmul(int nrow, int nk, int ncol, int mx1[][nk], int mx2[][ncol])
{
int x, i, j;
int *rst;
rst = malloc(sizeof(int) * nrow * ncol);

for(i = 0; i < nrow; i++)
{
for(j = 0; j < ncol; j++)
{
rst[i*ncol +j] =0;
for(x = 0; x < nk; x++)
{
rest[i*ncol +j] +=*(*(mx1+i)+x) * *(*(mx2+x)+j);
}
}
}
return rst;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
{
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
case DLL_THREAD_ATTACH:
{
break;
}
case DLL_THREAD_DETACH:
{
break;
}
}
}

mxmul.c和mxmul.h 编译后可以得到mxmul.dll和mxmul32.dll

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 用来封装.dll的python模块  cmxmul.py
import array
from cffi import FFI

def cmxmul(nrow, nk, ncol, mx1, mx2):
ffi = FFI()

# 基本类型参数关联
c_nrow = ffi.cast('int', nrow)
c_nk = ffi.cast('int', nk)
c_ncol = ffi.cast('int', ncol)

# 列表类型参数关联
_mx1 = array.array('l')
_mx2 = array.array('l')
[_mx1.fromlist(x) for x in mx1 ]
[_mx2.fromlist(x) for x in mx2 ]

# 数据的内存拷贝
c_mx1 = ffi.new('int[]', len(_mx1))
c_mx2 = ffi.new('int[]', len(_mx2))
ffi.memmove(c_mx1, _mx1, ffi.sizeof(c_mx1))
ffi.memmove(c_mx2, _mx2, ffi.sizeof(c_mx2))

# 声明函数
ffi.cdef('''
int *mxmul(int nrow, int nk, int ncol, int *mx1, int *mx2);
''')

# 调用dll库
try :
# 64位计算机加载mxmul.dll
C = ffi.dlopen('mxmul.dll')
except :
# 32位计算机加载mxmul32.dll
C = ffi.dlopen('mxmul32.dll')

# 调用动态链接库中的函数
c_res = C.mxmul(c_nrow, c_nk, c_ncol, c_mx1, c_mx2)

#解包返回
return ffi.unpack(c_res, nrow * ncol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 用来测试效果的python程序    test.py
from cmxmul import cmxmul

if __name__ == '__main__':
import time

nrow, nk, ncol = 500, 300, 500
mx1 = [[i for i in range(nk)] for j in range(nrow)]
mx2 = [[i for i in range(ncol)] for j in range(nk)]
start = time.perf_counter()
rst = cmxmul(nrow, nk, ncol, mx1, mx2)
end = time.perf_counter()
print('运行时间:%.4f秒' % (end - start))

'''
运行时间:0.3135秒
'''

13. 基于opencv的人脸识别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 在图像中识别并定位人脸
# 借助opencv-python
# 输入:一个图像
# 输出:一个带有人脸识别效果的图像

import cv2

fileName = 'face.jpg' # 待识别的文件名
markLineWeight = 2 # 标注框的宽度
markLineColor = (255, 0, 0) # 标注框的颜色


def main():
# 人脸识别引擎
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + \
'haarcascade_frontalface_default.xml')

# 读入文件,灰度转换
img = cv2.imread(fileName)
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 使用引擎探测多个面部
faces = face_cascade.detectMultiScale(imgGray, scaleFactor=1.3, minNeighbors=5)

# 标记已识别出的脸部
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), markLineColor, markLineWeight)

# 展示标记后的图片
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


if __name__ == '__main__':
main()